package com.leoncom.sms_silencer.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;

public class SmsLogger {

	public static void append(String message) {
		File file = openFile();
		if(file == null) {
			return;
		}
		
		try {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			FileWriter fw = new FileWriter(file, true);
			fw.append(sdf.format(date) + ":" + message + "\n");
			fw.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static File openFile() {
		String filePath =  Environment.getExternalStorageDirectory().toString() + "/SmsSilencer";
		String fileName = "SmsSilencer.log";
		File file = new File(filePath, fileName);
		if(file.getParentFile().exists() == false) {
			file.getParentFile().mkdirs();
		}
		if(file.exists() == false) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		return file;
	}
}
